home *** CD-ROM | disk | FTP | other *** search
- { DumbEdit - Multiple-window TransEdit Demonstration.}
-
- { The project should include DumbEdit.c (this file), TransEdit.c,}
- { FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)}
- { and MacTraps.}
-
- { 28 October 1986 Paul DuBois}
-
- { 10 January 1987 Ported to Lightspeed Pascal by Owen Hartnett }
- { ╜hm Software Co. 163 Richard Drive, Tiverton, RI 02878 }
-
-
- PROGRAM DumbEdit;
-
- USES
- TransEditPas, TransSkelPas;
-
- CONST
- maxSize = 8; { no. font sizes made available }
- hSize = 300; { horiz, vert size of new windows }
- vSize = 205;
- aboutAlrt = 1000;
-
- { File menu item numbers }
-
- new = 1; { begin new window }
- open = 2; { open existing file }
- close = 3; { close file }
- save = 5; { save file }
- saveas = 6; { save under another name }
- saveCopy = 7; { save a copy w/o switching file binding }
- revert = 8; { revert to version on disk }
- quit = 10;
-
- { Edit menu item numbers }
-
- undo = 1;
- cut = 3;
- copy = 4;
- paste = 5;
- clear = 6;
-
- { Format menu item numbers }
-
- wordWrap = 1;
- noWrap = 2;
- leftJust = 4;
- centerJust = 5;
- rightJust = 6;
-
- VAR
- lastFront : WindowPtr; { keeps track of front window }
- fileMenu, editMenu, fontMenu, sizeMenu, formatMenu : MenuHandle;
-
- sizes : ARRAY[0..maxSize] OF integer;
-
- { Uncheck all the items in a menu}
-
-
- PROCEDURE UncheckMenu (theMenu : MenuHandle);
- VAR
- i, nItems : integer;
-
- BEGIN
- nItems := CountMItems(theMenu);
-
- FOR i := 1 TO nItems DO
- BEGIN
- CheckItem(theMenu, i, false);
- SetItemStyle(theMenu, i, []);
- END;
- END;
-
- { Set the Font, Size and Format menus so that the items corresponding}
- { to the text characteristics of the window are checked. If the}
- { window isn't an edit window, dim all three menus.}
-
-
- PROCEDURE SetTextMenus (drawBar : Boolean);
-
- VAR
- theWind : WindowPtr;
- wFontName, mFontName : str255;
- i, nItems : integer;
- te : TEHandle;
-
- BEGIN
- theWind := Frontwindow;
- UncheckMenu(fontMenu); { toss current check marks }
- UncheckMenu(sizeMenu);
- UncheckMenu(formatMenu);
-
- IF NOT IsEWindow(theWind) THEN { disable the menus }
- BEGIN
- DisableItem(fontMenu, 0);
- DisableItem(sizeMenu, 0);
- DisableItem(formatMenu, 0);
- END
- ELSE
- BEGIN
- EnableItem(fontMenu, 0);
- EnableItem(sizeMenu, 0);
- EnableItem(formatMenu, 0);
-
- te := GetEWindowTE(theWind);
- IF te^^.crOnly < 0 THEN
-
- { Check appropriate word wrap item}
-
- CheckItem(formatMenu, noWrap, true)
- ELSE
- CheckItem(formatMenu, wordWrap, true);
-
- { Check appropriate justification item{}
-
- CASE te^^.just OF
- teJustLeft :
- CheckItem(formatMenu, leftJust, true);
- teJustRight :
- CheckItem(formatMenu, rightJust, true);
- teJustCenter :
- CheckItem(formatMenu, centerjust, true);
- OTHERWISE
- END;
-
- { Check appropriate font name item}
-
- FOR i := 0 TO maxSize - 1 DO
- BEGIN
- IF te^^.txSize = sizes[i] THEN
- checkitem(sizeMenu, i + 1, true);
- IF RealFont(te^^.txFont, sizes[i]) THEN
- SetItemStyle(sizeMenu, i + 1, [outline])
- ELSE
- SetItemStyle(sizeMenu, i + 1, []);
- GetFontName(te^^.txFont, wFontName);
- nItems := CountMItems(fontMenu);
- END;
- FOR i := 1 TO nItems - 1 DO
- BEGIN
- GetItem(fontmenu, i, mFontName);
- IF EqualString(wFontName, mFontName, false, true) THEN
- CheckItem(fontMenu, i, true);
- END;
- IF drawBar THEN
- DrawMenuBar;
- END;
- END;
-
- { Set File/Edit menu items according to type of front window.}
-
- { The general behavior is:}
-
- { New and Open always enabled, since a new edit window can always be}
- { opened.}
-
- { Close enabled when an edit or DA window in front (i.e., when there's}
- { a window at all).}
-
- { Save enabled for edit windows not bound to a file, and edit windows}
- { bound to a file when they're dirty (typed into, Edit menu used to}
- { do something to them).}
-
- { Save As and Save a Copy As enabled for edit windows.}
-
- { Revert enabled for edit windows bound to a file when they're dirty.}
-
- { Undo disabled when there's an edit window in front.}
-
- PROCEDURE SetNonTextmenus;
-
- VAR
- theWind : WindowPtr;
- theKind : integer;
- thePeek : windowPeek;
-
- BEGIN
- DisableItem(fileMenu, close); { assume no window at all }
- DisableItem(fileMenu, save);
- DisableItem(fileMenu, saveas);
- DisableItem(fileMenu, savecopy);
- DisableItem(fileMenu, revert);
- EnableItem(editMenu, undo);
-
- theKind := 0;
- theWind := FrontWindow;
- thePeek := WindowPeek(theWind);
- IF theWind <> NIL THEN
- theKind := thePeek^.windowKind;
- IF theKind < 0 THEN { DA in front }
- EnableItem(fileMenu, close)
- ELSE IF IsEWindow(theWind) THEN { edit window in front }
- BEGIN
- EnableItem(fileMenu, close);
- EnableItem(fileMenu, saveas);
- EnableItem(fileMenu, savecopy);
- IF (GetEWindowFile(theWind, NIL) = false) THEN { not bound to file }
- EnableItem(fileMenu, save)
- ELSE IF IsEWindowDirty(theWind) THEN { bound - is it dirty? }
- BEGIN
- EnableItem(fileMenu, save);
- EnableItem(fileMenu, revert);
- END;
- DisableItem(editMenu, undo);
- END;
- END;
-
- { Background procedure. Check front window, reset menus if it}
- { changes. The menu bar doesn't need redrawing by SetTextMenus}
- { if the previous and current front window are either both edit}
- { windows or both not edit windows. This check eliminates some}
- { needless menu flashing.}
-
- PROCEDURE CheckFront;
-
- BEGIN
- IF FrontWindow <> lastFront THEN
- BEGIN
- SetNonTextMenus;
- IF IsEWindow(FrontWindow) = IsEwindow(lastFront) THEN
- SetTextmenus(false)
- ELSE
- SetTextmenus(true);
- lastFront := FrontWindow;
- END;
- END;
-
- { Got an activate or deactivate. It doesn't matter which, really.}
- { Set the text menus appropriately for the front window, and draw}
- { the menu bar, as these menus might change state from enabled to}
- { disabled or vice-versa.}
-
-
- PROCEDURE Activate (active : Boolean);
- BEGIN
- CheckFront;
- END;
-
- { Got a keyclick in an edit window.}
-
-
- PROCEDURE Key;
- BEGIN
- SetNonTextMenus;
- END;
-
- { Close selected from File menu, or close box of edit window}
- { clicked.}
-
- PROCEDURE myClose;
- VAR
- theWind : WindowPtr;
- ignore : Boolean;
-
- BEGIN
- GetPort(theWind);
- ignore := EWindowClose(theWind);
- CheckFront;
- END;
-
- PROCEDURE MakeWind (bindToFile : Boolean);
-
- VAR
- r : Rect;
- windCount : integer;
- offset : integer;
- ignore : WindowPtr;
- BEGIN
- windCount := 0;
- IF FrontWindow = NIL THEN
- windCount := 0;
- SetRect(r, 0, 0, hSize, vSize);
- windCount := windCount + 1;
- offset := 50 + (25 * (windCount MOD 4));
- OffsetRect(r, offset, offset);
- ignore := NewEWindow(r, '', true, WindowPtr(-1), true, longint(0), bindToFile);
- END;
-
- { File menu handler}
-
-
- PROCEDURE DoFileMenu (item : integer);
-
- VAR
- theWind : WindowPtr;
- mypeek : WindowPeek;
- ignore : Boolean;
- BEGIN
- theWind := FrontWindow;
- CASE item OF
- new :
- MakeWind(false);
- open :
- MakeWind(true);
- close :
- IF ISEWindow(theWind) THEN
- ignore := EWindowClose(theWind)
- ELSE
- BEGIN
- mypeek := WindowPeek(theWind);
- CloseDeskAcc(mypeek^.windowKind); { DA in front }
- END;
- save :
- ignore := EWindowSave(theWind);
- saveas :
- ignore := EWindowSaveAs(theWind);
- revert :
- ignore := EWindowRevert(theWind);
- quit :
- IF ClobberEWindows = true THEN
- SkelWhoa;
- OTHERWISE
- END;
- SetNonTextMenus;
- END;
-
- { Handle Font menu items}
-
-
- PROCEDURE DoFontMenu (item : integer);
-
- VAR
- font : integer;
- te : TEHandle;
- theWind : WindowPtr;
- theFontName : Str255;
-
- BEGIN
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- IF te <> NIL THEN { not an edit window }
- BEGIN
- GetItem(fontMenu, item, theFontName);
- GetFNum(theFontName, font);
- SetEWindowSTyle(theWind, font, te^^.txSize, te^^.crOnly, te^^.just);
- SetTExtMenus(false);
- END;
- END;
-
- { Handle Size menu items }
-
- PROCEDURE DoSizeMenu (item : integer);
-
- VAR
- te : TEHandle;
- theWind : WindowPtr;
- BEGIN
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- IF te <> NIL THEN
- BEGIN
- SetEWindowStyle(theWind, te^^.txFont, sizes[item - 1], te^^.crOnly, te^^.just);
- SetTextMenus(false);
- END;
- END;
-
- { Handle Format menu items }
-
- PROCEDURE DoFormatMenu (item : integer);
-
- VAR
- font, size, just, wrap : integer;
- te : TEHandle;
- theWind : WindowPtr;
-
- BEGIN
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- IF te <> NIL THEN { an edit window }
- BEGIN
- font := te^^.txFont;
- size := te^^.txSize;
- just := te^^.just;
- wrap := te^^.crOnly;
- CASE item OF
- wordWrap :
- wrap := 0;
- noWrap :
- wrap := -1;
- leftJust :
- just := teJustLeft;
- centerJust :
- just := teJustCenter;
- rightJust :
- just := teJustRight;
- OTHERWISE
- END;
- SetEWindowStyle(theWind, font, size, wrap, just);
- SetTextMenus(false);
- END;
- END;
-
- PROCEDURE DoAbout;
- VAR
- ignore : integer;
- BEGIN
- ignore := Alert(aboutAlrt, NIL);
- END;
-
- { Initialize TransSkel, create menus and install handlers.}
-
-
- BEGIN
- lastFront := NIL;
- sizes[0] := 9;
- sizes[1] := 10;
- sizes[2] := 12;
- sizes[3] := 14;
- sizes[4] := 18;
- sizes[5] := 20;
- sizes[6] := 24;
- sizes[7] := 48;
- SkelInit;
- TransEditInit;
- SkelApple('About DumbEdit...', @DoAbout);
-
- fileMenu := NewMenu(1000, 'File');
- AppendMenu(fileMenu, 'New/N;Open.../O;Close/K;(-;Save/S;Save As...');
- AppendMenu(fileMenu, 'Save a Copy As...;Revert/R;(-;Quit/Q');
- SkelMenu(fileMenu, @DoFileMenu, NIL);
-
- editMenu := NewMenu(1001, 'Edit');
- AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
- SkelMenu(editMenu, @EWindowEditOp, NIL);
-
- fontMenu := NewMenu(1002, 'Font');
- DisAbleItem(fontMenu, 0);
- AddREsMenu(fontMenu, 'FONT');
- SkelMenu(fontMenu, @DoFontMenu, NIL);
- sizeMenu := NewMenu(1003, 'Size');
- DisableItem(sizeMenu, 0);
- AppendMenu(sizemenu, '9 Point;10 Point;12 Point;14 Point');
- AppendMenu(sizeMenu, '18 Point;20 Point;24 Point;48 Point');
- SkelMenu(sizeMenu, @DoSizeMenu, NIL);
-
- formatMenu := NewMenu(1004, 'Format');
- DisableItem(formatMenu, 0);
- AppendMenu(formatMenu, 'Word Wrap;No Word Wrap;(-;Left;Center;Right');
- Skelmenu(formatMenu, @DoFormatMenu, NIL);
-
- SetNonTextMenus;
- SetTextMenus(true);
-
- { Do TransEdit-specific setup: set creator for any files created,}
- { set default text style and event notification procedures for}
- { new windows.}
-
- SetEWindowCreator('DUMB');
- SetEWindowStyle(NIL, monaco, 9, 0, teJustLeft);
- SetEWindowProcs(NIL, @Key, @Activate, @myClose);
-
- { Process events until user quits,}
- { then clean up and exit}
-
- SkelBackground(@CheckFront);
- SkelMain;
- SkelClobber;
- END.